import type { RouteRecordRaw } from "vue-router";
import { createRouter, createWebHashHistory } from "vue-router";
import defaultRoutes from "./defaultRoutes";

const router = createRouter({
  history: createWebHashHistory(),
  scrollBehavior (to, from, savedPosition) {
    return { top: 0 }
  },
  routes: [...defaultRoutes] as RouteRecordRaw[],
});
router.beforeEach(async (to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.meta.isLogin) {
    if (token) {
      next();
    } else {
      next({
        path: "/",
      });
    }
  } else {
    next();
  }
})

export default router;
